package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.entity.CampaignInstance;
import com.ddyx.entity.CampaignTask;
import com.ddyx.mapper.CampaignInstanceMapper;
import com.ddyx.mapper.CampaignTaskMapper;
import com.ddyx.service.CampaignInstanceService;
import com.ddyx.util.CalculateUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.vo.CampaignVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CampaignInstanceServiceImpl implements CampaignInstanceService {
	
	private static final  ObjectMapper om = new ObjectMapper();
	
	@Resource
	CampaignInstanceMapper campaignInstanceMapper;
	
	@Resource
	CampaignTaskMapper campaignTaskMapper;
	
	/* 
	 * 红包页面展示信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm showBonusPage(RequestForm param) {
		ResponseForm result = new ResponseForm();
//		CampaignVo campaignVo = new CampaignVo();
		CampaignInstance campaignInstance = new CampaignInstance();
		CampaignTask campaignTask = new CampaignTask();
		Map<String , Object> paramMap  = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(campaignInstance, paramMap);
			MyBeanUtils.populate(campaignTask, paramMap);
			campaignInstance = campaignInstanceMapper.selectOne(campaignInstance);
			List<CampaignTask> friendList = campaignTaskMapper.select(campaignTask);
			campaignInstance.setFriendList(friendList);
//			campaignVo.setCampaignInstance(campaignInstance);
//			campaignVo.setFriendList(friendList);
//			result.setData(campaignVo);
			result.setData(campaignInstance);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	/* 
	 * 新增开团
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertCampaignInstance(RequestForm param) {
		ResponseForm result = new ResponseForm();
//		CampaignVo campaignVo = new CampaignVo();
//		CampaignTask campaignTask = new CampaignTask();
		CampaignInstance campaignInstance = new CampaignInstance(); 
		Map<String , Object> paramMap  = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(campaignInstance, paramMap);
			
			campaignInstance.setInvalidStatus(0); //设置默认过期标志为0:未过期
			campaignInstance.setBonusStatus(0); //默认为0 :未拆开
			campaignInstance.setLimitCount(4);	//设置红包限额 默认为4
			campaignInstance.setBeginTime(new Date());//设置开始 时间 当前时间
			
			campaignInstance.setEndTime(CalculateUtil.dateAddOne(campaignInstance.getBeginTime()));//设置结束时间 间隔一天
			campaignInstanceMapper.insertSelective(campaignInstance);
//			campaignInstance = campaignInstanceMapper.select(campaignInstance);
			
			//发送消息队列
//			this.sendMsg(campaignInstance);
			CampaignTask campaignTask = new CampaignTask();
			campaignTask.setInstanceId(campaignInstance.getInstanceId());
			campaignTask.setJoinTime(new Date());
			campaignTask.setHeadImage(campaignInstance.getHeadImage());
			campaignTask.setNickName(campaignInstance.getNickName());
			campaignTask.setWxUid(campaignInstance.getWxUid());
			campaignTaskMapper.insertSelective(campaignTask);
			
			CampaignTask campaignTask2 = new CampaignTask();
			campaignTask2.setInstanceId(campaignInstance.getInstanceId());
			List<CampaignTask> friendList = campaignTaskMapper.select(campaignTask2);
			
			
			campaignInstance.setFriendList(friendList);
//			campaignVo.setCampaignInstance(campaignInstance);
//			campaignVo.setFriendList(friendList);
//			result.setData(campaignVo);
			result.setData(campaignInstance);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	
	
	 //MQ发送消息
	/* private void sendMsg(CampaignInstance campaignInstance) {
	 Map<String, Object> map = new HashMap<>();
	 try {
	 Session session = MQSessionUtil.getSession(); //获取session
	 Topic topic = session.createTopic("HB"); //创建'HB' topic 队列
	 MessageProducer producer = session.createProducer(topic); //创建生产者
	 MapMessage msg = session.createMapMessage();
	 msg.setInt("instanceId", campaignInstance.getInstanceId());
	 msg.setString("wxUid", campaignInstance.getWxUid());
	 msg.setString("nickName", campaignInstance.getNickName());
	 msg.setString("headImage", campaignInstance.getHeadImage());
	 } catch (Exception e) {
	 e.printStackTrace();
	 }
	 }*/


	


}
